// JavaScript Document version: 5 -------------written by Vaibhav Shetye

// Global Variables
var t;
var tabs =6;
var cross = new Array();

//---------------------------------------------------------------------------------------------------------------------

// Funtion to get cross values from other function
function setCross(arbit)
{
    this.cross = arbit;
}
// Funtion to set the value of T used to set tId
function setT(n)
{
    this.t = n;
}

// Function to add task only when Enter key is pressed
function whenEnter(e)
{
    var temp = whichKey(e);  // get which key is pressed
    
    if (temp == 13) // add the task when enter is pressed --- code for enterkey is 13
    {
        addTask();  
    }   
}

// Function to add task
function addTask()
{
    // get Task and all related attributes
    var task        = document.getElementById('newTask').value;
    var priority    = document.getElementById('priority').value;
    var dLine       = document.getElementById('datebox').value;
    var tStamp      = getTS();
    // if DeadLine Val is empty
    
    if(dLine!=""&&dLine!=" ")
    {
        dLine = document.getElementById('datebox').value; 
    }
    else
    {
        dLine = "None";
    }
    
    // Unique id for each task
    var tId         = "T"+t;  
    
    // resetting the textboxes
    document.getElementById('newTask').value    = "";
    document.getElementById('datebox').value    = "";
    
//    alert("Tid:"+tId+"   Task: "+task+" Priority: "+priority+" DeadLine: "+dLine);


    if(task!=""&&task!=" ")     // Add task only if task is not empty
    {
        // getting object to reference a row 
        var tbl = document.getElementById("tasks").insertRow(1);
        
        // getting objects to reference cloumns
		var sr_no = tbl.insertCell(0);
		var tsk = tbl.insertCell(1);
		var stat = tbl.insertCell(2);
		var prio = tbl.insertCell(3);
		var ts = tbl.insertCell(4);
		var dl = tbl.insertCell(5);
		var cng = tbl.insertCell(6);
		var rmov = tbl.insertCell(7);
        
        // Setting Row ID for unique reference
		tbl.id =  tId;
        
        // Inserting Task data into the cells
		sr_no.innerHTML = "<b>"+adjustVal(t)+"</b>";  // Calls the function to adjust value of i
		tsk.innerHTML = "<b>"+task+"</b>";
		stat.innerHTML = "<b>Pending</b>";
		prio.innerHTML = "<b>"+priority+"</b>";
		ts.innerHTML = "<b>"+tStamp+"</b>";		// The function getTS() is called to get the timestamp value
		dl.innerHTML = "<b>"+dLine+"</b>";
		cng.innerHTML = "<b>Complete</b>";
		rmov.innerHTML ="<img src='images/Del.png' height='20' width='50'>";
        			
		// Setting adjacency matrix value to nc="not crossed". Representing New Task
        cross[tbl.id]="nc";
        
        // Incrementing "t" to point to next row
        t++;	       
    }
}

//========================================================================================================

//	Functions to Manipulate the task List
//-----------------------------------------------------------------------------------------------------------

// Functions to manupulate the tasks using Manupulation Engine
function manuEngine()
{
	cell = whichCol();
	row = whichRow();
	//alert(" Cell = "+cell+"  Row = "+row);
	
	switch(cell)
	{
        case 0:  alert("Row Id = "+getRowId());
        break;
		case 2: crossTask(row);
		break;
		case 6: clickTo(row);
		break;
		case 7: rmov(row);
		break;
		default: break;
	}
}

// Function to remove a task
function rmov(row)
{
	document.getElementById("tasks").deleteRow(row);
}

// Function to change status of the task to complete
function crossTask(row)
{
	var y = document.getElementById("tasks").rows[row];
	y.cells[2].innerHTML="<b>Complete</b>";
	for(k=0;k<tabs;k++)		                              // Crossing out text inside the cells
	{
		temp = y.cells[k].innerHTML;
		temp="<del>"+temp+"</del>";
		y.cells[k].innerHTML = temp;
	}
	cross[y.id]="c";
	y.style.color="#6495ED";
	y.cells[6].innerHTML = "<b>undo</b> ";
	y.cells[6].style.color="red";
	//y.cells[7].style.color="#7CFC00";
	//alert(cross[y.id]);
}

// Function that implements undo
function clickTo(row)
{
	tId = getRowId();

	if(cross[tId]=="nc")
	{
		//alert(" Cross Task selected  = "+cross[id]);
		crossTask(row);
	}
	else
	{
		//alert(" Undo selected  = "+cross[id]);
		undo(row);
	}
}

// Function to undo recently crossed task
function undo(row)
{
	var y = document.getElementById("tasks").rows[row];
	for(k=0;k<tabs;k++)									// undo Crossing out text inside the cells
	{
		temp = y.cells[k].innerHTML;
		buff = temp.length-11;
		//alert(" end = "+buff);
		temp=temp.substr(5,buff);
		y.cells[k].innerHTML = temp;
	}
	y.cells[2].innerHTML="<b>Pending</b>";
	y.cells[6].innerHTML="<b>Complete</b>";
	y.style.color="black";
	cross[y.id]="nc";
}
//=================================================================================================================
// To manage visual effects
//---------------------------------------------
function onIt()
{
    var tId = getRowId();
    if(cross[tId]=="nc")    // if task is not crossed then highlight
    {
        hiLite();
    }
}

function outIt()
{
    var tId = getRowId();   // if task is not crossed the dehighlite
    if(cross[tId]=="nc")
    {
        deLite();
    }
}
